package com.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.service.ICommonDataService;
import com.server.service.IMngMenuService;
import com.server.web.SessionUtil;

@Service("MngMenuService")
public class MngMenuService implements IMngMenuService{
	@Autowired
	private ICommonDataService commonDataService; 
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public Boolean addMenuSecond(String name, String function, int p_id, int sort,String operator){
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("name", name);
		inputMap.put("function", function);
		inputMap.put("p_id", p_id);
		inputMap.put("sort", sort);
		Boolean isSuccess = commonDataService.add(operator, "education.admin_menu", inputMap);
		return isSuccess;
	}

	@Override
	public List<Map<String, Object>> menuList() {
		List<Map<String, Object>> menus = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> tempList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> menuList = new ArrayList<Map<String, Object>>();
		String sql1 = "select * from education.admin_menu where valid=1 order by sort asc";
		menus= jdbcTemplate.queryForList(sql1);
		Map<String, Object> tempMap = new HashMap<String, Object>();
		for(Map<String, Object> menusArrTem: menus){
			int p_id = (int) menusArrTem.get("p_id");
			if(p_id==0){
				tempList.add(menusArrTem);
			}
		}
		Map<String, Object> menuSubMap = new HashMap<String, Object>();
		if(tempList.size()>0){
		for (int i = 0; i < tempList.size(); i++) {
			Map<String, Object> menuMap = tempList.get(i);
			tempMap = menuMap;
			List<Map<String, Object>> subTempList = new ArrayList<Map<String,Object>>();
			for (int j = 0; j < menus.size(); j++) {
				
				Map<String, Object> subMap = menus.get(j);
				if(menuMap.get("id")==subMap.get("p_id")){
					
					subTempList.add(subMap);
					
				}
			}
			tempMap.put("menu_sub", subTempList);
			menuList.add(tempMap);
		}
		}
		return menuList;
	}

	@Override
	public Boolean addMenuFirst(String name, int sort, String operator) {
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("name", name);
		inputMap.put("sort", sort);
		inputMap.put("p_id", 0);
		Boolean isSuccess = commonDataService.add(operator, "education.admin_menu", inputMap);
		return isSuccess;
	}

	@Override
	public List<Map<String, Object>> getFirstMenu() {
		String sql = "select * from education.admin_menu where p_id=0 and valid=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public Map<String, Object> getMenuById(int id) {
		String sql = "select * from education.admin_menu where id='%d'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, id));
		Map<String, Object> menuMap = new HashMap<String, Object>();
		if(list.size()>0){
			menuMap = list.get(0); 
		}
		
		return menuMap;
	}

	@Override
	public Boolean updateMenu(Map<String, Object> inputMap,String operator,String field,String value) {
		
		Boolean isSuccess = commonDataService.update(operator, "education.admin_menu", field, value, inputMap);
		
		return isSuccess;
	}

	@Override
	public Boolean deleteMenu(String field,String value) {
		Boolean isSuccess = false;
		String sql = "select * from education.admin_menu where id=%d";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql,Integer.parseInt(value)));
		if(list.size()>0){
			Map<String, Object> map = list.get(0);
			if((int) map.get("p_id")==0){
				isSuccess = commonDataService.delete("education.admin_menu", "p_id", value);
				if(isSuccess){
					isSuccess = commonDataService.delete("education.admin_menu", field, value);
				}
			}else{
				isSuccess = commonDataService.delete("education.admin_menu", field, value);
			}
		}
		
		return isSuccess;
	}
	
}
